package com.younion.action.consumer.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.ICardEntityService;
import com.younion.business.IFavoritesService;
import com.younion.business.IMemberCardService;
import com.younion.common.constants.SysConstants;
import com.younion.model.CardsListResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.API_CardEntityVO;
import com.younion.vo.API_MemberCardVO;
import com.younion.vo.FavoritesVO;

public class CardListGetConsumer extends AbstractRequestConsumer {
	private ICardEntityService cardEntityService;
	private IMemberCardService memberCardService;
	private IFavoritesService favoritesService;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getCardList(request, response);
	}

	public String getCardList(HttpServletRequest request,
			HttpServletResponse response) {
		String user_token = request.getParameter("user_token");
		// 搜索
		String searchType = request.getParameter("search_type");
		String searchKey = request.getParameter("search_key");
		String cityId = request.getParameter("city_id");
		Map paramMap = new HashMap();
		List<CardsListResult> cardsListResultList = new ArrayList<CardsListResult>();
		List<API_CardEntityVO> api_CardEntityVOList = new ArrayList<API_CardEntityVO>();
		List<API_MemberCardVO> api_MemberCardVOList = null;
		ResultObject resultObject = new ResultObject();
		List<FavoritesVO> favoritesVOList = null;
		try {
			if (HttpRequestUtils.checkIsEmptyParam("search_type", searchType,
					response)) {
				if (!HttpRequestUtils.checkNeedIntegerParam("search_type",
						searchType, response))
					return null;
			}
			paramMap.put("searchType", searchType);
			paramMap.put("searchKey", searchKey);
			paramMap.put("cityId", cityId);
			api_CardEntityVOList = cardEntityService
					.selectAllByChannel(paramMap);
			// 会员登录情况下获取会员卡需要查询会员所拥有的卡
			if (StringUtils.isNotEmpty(user_token)) {// 登录的情况下
				api_MemberCardVOList = (List<API_MemberCardVO>) memberCardService
						.getMyCardByUserToken(user_token);
				Map favoritesParamMap = new HashMap();
				favoritesParamMap.put("userToken", user_token);
				favoritesVOList = (List<FavoritesVO>) favoritesService
						.getByMap(favoritesParamMap);
			}
			this.processCardEntityData(api_CardEntityVOList,
					api_MemberCardVOList, favoritesVOList, cardsListResultList);
			resultObject.setResult(cardsListResultList);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}

		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	private void processCardEntityData(
			List<API_CardEntityVO> api_CardEntityVOList,
			List<API_MemberCardVO> api_MemberCardVOList,
			List<FavoritesVO> favoritesVOList,
			List<CardsListResult> cardsListResultList) {
		String img_url_pre = ConfigHelper.fastGetConfig("interface",
				"img_url_pre", "http://localhost:8080/younonmember/");
		for (API_CardEntityVO api_cardEntity : api_CardEntityVOList) {
			CardsListResult cardsListResult = new CardsListResult();
			cardsListResult.setCard_id(new String(api_cardEntity.getId()
					.toString()));
			cardsListResult.setCard_name(api_cardEntity.getCardName());
			cardsListResult.setCategory_id(api_cardEntity.getCustomerVO()
					.getCustomerKindId().toString());
			cardsListResult.setIs_follow("0");
			cardsListResult.setIs_league(api_cardEntity.getCustomerVO()
					.getHaveAllianceFavourable().toString());
			cardsListResult.setIs_store_club(api_cardEntity.getCustomerVO()
					.getIsCoreCustomer().toString());
			cardsListResult.setActivatedPic(img_url_pre
					+ SysConstants.CARD_IMG_URL + "/" + api_cardEntity.getId()
					+ "/" + api_cardEntity.getActivatedPic());
			cardsListResult.setNonactivatedPic(img_url_pre
					+ SysConstants.CARD_IMG_URL + "/" + api_cardEntity.getId()
					+ "/" + api_cardEntity.getNonactivatedPic());
			cardsListResult.setShop(api_cardEntity.getCustomerVO().getShops());
			cardsListResult.setNearby("1000");// TODO 需要配置附近的距离范围
			cardsListResult.setIs_new("1");// TODO 需要算出时间最新的
			cardsListResult.setIs_hot(api_cardEntity.getIsHot().toString());
			cardsListResult.setIs_recommend(api_cardEntity.getIsRecommend()
					.toString());
			cardsListResult.setCard_content(api_cardEntity.getWelfare());
			String user_card = "";
			if (api_MemberCardVOList != null && api_MemberCardVOList.size() > 0) {
				for (API_MemberCardVO api_MemberCardVO : api_MemberCardVOList) {
					if (api_MemberCardVO.getApi_CardEntityVO().getId().equals(
							api_cardEntity.getId())) {
						user_card = api_MemberCardVO.getId().toString();
					}
				}
			}
			cardsListResult.setUser_card(user_card);
			if (favoritesVOList != null) {
				for (FavoritesVO favoritesVO : favoritesVOList) {
					if (favoritesVO.getFavourId() .equals( api_cardEntity.getId())) {
						cardsListResult.setIs_follow("1");
					}
				}
			}
			cardsListResult.setHave_favourable(api_cardEntity.getCustomerVO()
					.getHaveFavourable().toString());
			cardsListResult
					.setAgioInfo(api_cardEntity.getAgioInfo() == null ? ""
							: api_cardEntity.getAgioInfo());
			cardsListResultList.add(cardsListResult);
		}
	}

	public void setCardEntityService(ICardEntityService cardEntityService) {
		this.cardEntityService = cardEntityService;
	}

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}

	public void setFavoritesService(IFavoritesService favoritesService) {
		this.favoritesService = favoritesService;
	}
}
